const moment = require('moment');

module.exports = (db, DataTypes) => {
  // 鉴于申请单不会特别多，可以牺牲一部分硬盘空间，所以合到一个表内
  return db.define('RequestNote', {
    // common
    oid: {
      type: DataTypes.STRING,
      unique: true,
      allowNull: false,
      set(index) {
        let date = moment();
        let year = date.year();
        let month = date.month() + 1;
        let day = date.date();
        let x = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
        let postfix = '';
        postfix += x.charAt(Math.ceil(Math.random()*100000000) % x.length);
        postfix += x.charAt(Math.ceil(Math.random()*100000000) % x.length);
        postfix += index;  // 当天的index
        let oid = `CR${year}${month > 9 ? month : '0' + month}${day > 9 ? day : '0' + month}${postfix}`;
        this.setDataValue('oid', oid);
      },
    },
    type: DataTypes.STRING,  // ['Construction', 'RequestChange', 'ExternalTest', 'Maintain', 'ProjectTest']
    title: DataTypes.STRING,
    applicantName: DataTypes.STRING,
    departmentName: DataTypes.STRING,
    applicantPhone: DataTypes.STRING,
    // 处于第几步
    step: DataTypes.INTEGER,

    // 拒绝的情况
    isRejected: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
    },
    reason: DataTypes.TEXT,

    // 对于用户来说申请单状态
    userStatus: DataTypes.STRING,
    // 对于审批员来说申请单状态，一个申请单可以有多个审批员在审批，所以这里不能用一个字段
    // 应该是直接判断出来，如果有审批记录就是已审批，没有审批记录就是未审批
    // approvalStatus: DataTypes.STRING,

    createdTime: {
      type: DataTypes.STRING,
      set(value) {
        let date = moment();
        let year = date.year();
        let month = date.month() + 1;
        let day = date.date();   // todo: momentjs里面要获取某月的某一天要用date，注意！
        this.setDataValue('createdTime', `${year}年${month}月${day}日`);
      }
    },

    // ExternalTest
    // applicantDate: DataTypes.STRING,
    filename: DataTypes.STRING,
    fileUrl: DataTypes.STRING,

    // Construction
    projectName: DataTypes.STRING,
    beginTime: DataTypes.STRING,
    endTime: DataTypes.STRING,
    unitName: DataTypes.STRING,
    contactName: DataTypes.STRING,
    operationArea: DataTypes.TEXT,
    projectDescription: DataTypes.TEXT,
    isInfluenceSystemRun: DataTypes.STRING,
    isInfluenceSystemRunInfo: DataTypes.TEXT,
    isChangeSystem: DataTypes.STRING,
    isChangeSystemInfo: DataTypes.TEXT,
    isHaveRisk: DataTypes.STRING,
    isHaveRiskInfo: DataTypes.TEXT,
    cooperateInfo: DataTypes.TEXT,

    // RequestChange
    systemClass: DataTypes.STRING,
    changeClass: DataTypes.STRING,
    changeProjectName: DataTypes.STRING,
    beforeChangeProjectDescription: DataTypes.TEXT,
    afterChangeDescription: DataTypes.TEXT,
    influenceScope: DataTypes.TEXT,
    requestChangeDate: DataTypes.STRING,
    requestChangeTime: DataTypes.STRING,
    isPlanStop: DataTypes.STRING,
    isPlanStopInfo: DataTypes.TEXT,
    isInfluenceRun: DataTypes.STRING,
    isInfluenceRunInfo: DataTypes.TEXT,
    isNeedCooperate: DataTypes.STRING,
    isNeedCooperateInfo: DataTypes.TEXT,
    isHaveGaiyao: DataTypes.STRING,
    isHaveGaiyaoInfo: DataTypes.TEXT,
    isHaveCaozuo: DataTypes.STRING,
    isHaveCaozuoInfo: DataTypes.TEXT,
    isHaveFengxian: DataTypes.STRING,
    isHaveFengxianInfo: DataTypes.TEXT,
    isHaveHuitui: DataTypes.STRING,
    isHaveHuituiInfo: DataTypes.TEXT,
    isHaveYingji: DataTypes.STRING,
    isHaveYingjiInfo: DataTypes.TEXT,
    isHaveCeshi: DataTypes.STRING,
    isHaveCeshiInfo: DataTypes.TEXT,
    isHaveOtherFiles: DataTypes.STRING,
    isHaveOtherFilesInfo: DataTypes.TEXT,
    onSiteDescription: DataTypes.TEXT,
    onSiteTestDescription: DataTypes.TEXT,
    onSiteTestResult: DataTypes.TEXT,
    onSiteSupervisor: DataTypes.TEXT,
    verifyContent: DataTypes.TEXT,
    reviewCode: DataTypes.STRING,
    reviewCloseCode: DataTypes.STRING,
    reviewExplain: DataTypes.TEXT,
    changeImplementorName: DataTypes.STRING,
    changeImplementTime: DataTypes.DATE,

    // Maintain
    applicantDate: DataTypes.STRING,
    // beginTime: DataTypes.STRING,
    // endTime: DataTypes.STRING,
    beginTimeConfirm: DataTypes.STRING,
    endTimeConfirm: DataTypes.STRING,
    applicationReason: DataTypes.TEXT,
    operationOverview: DataTypes.TEXT,
    // isInfluenceSystemRun: DataTypes.STRING,
    // isInfluenceSystemRunInfo: DataTypes.TEXT,
    isInfluenceThirdParty: DataTypes.STRING,
    isInfluenceThirdPartyInfo: DataTypes.TEXT,
    isNeedEmergencyOperation: DataTypes.STRING,
    isNeedEmergencyOperationInfo: DataTypes.TEXT,
    isNeedEmergencyPrepare: DataTypes.STRING,
    isNeedEmergencyPrepareInfo: DataTypes.TEXT,
    isNeedTest: DataTypes.STRING,
    isNeedTestInfo: DataTypes.TEXT,
    testResult: DataTypes.TEXT,
    dutyShiftLeader: DataTypes.STRING,
    maintainLeader: DataTypes.STRING,
    groupLeader: DataTypes.STRING,

    // ProjectTest
    planTime: DataTypes.STRING,
    testAimAndDescription: DataTypes.TEXT,
    // isInfluenceSystemRun: DataTypes.STRING,
    // isInfluenceSystemRunInfo: DataTypes.TEXT,
    // isInfluenceThirdParty: DataTypes.STRING,
    // isInfluenceThirdPartyInfo: DataTypes.TEXT,
    isNeedCoherentUnit: DataTypes.STRING,
    isNeedCoherentUnitInfo: DataTypes.TEXT,
    planTestStep: DataTypes.TEXT,
    // filename: DataTypes.STRING,
    // fileUrl: DataTypes.STRING,

    createdAt: DataTypes.DATE,
    updatedAt: DataTypes.DATE,
  }, {
    timestamps: true,
    associate(models) {
      models.RequestNote.belongsToMany(models.User, {
        through: {
          model: 'UserRequestNote',
          unique: false,
        },
        constraints: false,
        foreignKey: "RequestNoteId",
      }, {

      });  // 申请与审批记录表
      // models.RequestNote.belongsTo(models.UserRequestNote,  { foreignKey: 'RequestNoteId' })  // 这种写法错误！
    }
  })
};
